﻿#include "Utils.h"
#include "lineEndChanger.h"
#include "define.h"

string& Utils::strAddLN(string &str){
	str += "\n";
	return str;
}

string Utils::strAddLN(const string str){
	string strReturn(str);
	strReturn += "\n";
	return strReturn;
}

string& Utils::strReplaceAll(string &str,string &toFind,string &toReplace){
	string::size_type pos;
	string::size_type off = 0;
	string::size_type repSize = toReplace.size();
	string::size_type fndSize = toFind.size();
	while((pos = str.find(toFind,off))!=string::npos){
		str.replace(pos,fndSize,toReplace);
		off = pos + repSize;
	}
	return str;
}

string& Utils::strReplaceAll(string &str,const string &toFind,const string &toReplace){
	string::size_type pos;
	string::size_type off = 0;
	string::size_type repSize = toReplace.size();
	string::size_type fndSize = toFind.size();
	while((pos = str.find(toFind,off))!=string::npos){
		str.replace(pos,fndSize,toReplace);
		off = pos + repSize;
	}
	return str;
}

//使用重命名的方式判断文件是否存在
bool Utils::fileExist(const char* fileName){
	if(rename(fileName, fileName)){
		return false;
	}else{
		return true;
	}
}
void Utils::showVector(vector<int> &iv){
	 vector<int>::iterator i = iv.begin();
	  cout << "======beginning of vector======" <<endl;
	  while(i!=iv.end())
		  cout<<*i++<<endl;
	  cout << "=========end of vector=========" <<endl;
};

void Utils::showVector(vector<string> &sv){
	  vector<string>::iterator i = sv.begin();
	  cout << "======beginning of vector======" <<endl;
	  while(i!=sv.end())
		  cout<<*i++<<endl;
	  cout << "=========end of vector=========" <<endl;
};

#ifdef MAIN_CLASS_UTIL
int main(){
#else
int main_debug(){
#endif
	string in;
	string find;
	string repl;
	while(true){
		cout << "input in:" << endl;
		cin >> in;
		cout << "input find:" << endl;
		cin >> find;
		cout << "input repl:" << endl;
		cin >> repl;

		Utils::strReplaceAll(in,find,repl);

		cout << "===result====" << endl;
		cout << in << endl;
#ifdef WINDOWS
		system("pause");
#endif
	}
}

